import { isLogin, toLogin, getUserInfo, setUserInfo } from '../../utils/auth'
import { showModel, showNotify, showToast, showDialog } from '../../utils/index'
import http from '../../utils/http'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
        isLogin: false,
        
        // 购物车
        cart: null,

        // 未选中商品
        nullSelect: false,

        allChecked: false
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
        // 用户登录信息
        this.setData({ userInfo: getUserInfo(), isLogin: isLogin() })

        if(this.data.isLogin) {
            this.getCarts()
        }
	},

    /**
     * 获取购物车数据
     */
    async getCarts() {
        let { userInfo } = this.data
        let res = await http.request('/api/mall/cart/getCart/'+userInfo.id)
        console.log(res);
        if(res.status) {
            this.setData({ cart: res.data })
            
            // 判断是否全部选中
            let checkAll = res.data.items.filter(v => v.check == false)
            if(checkAll.length == 0) {
                this.setData({ allChecked: true })
            }
        }
        
    },

    /**
     * 去结算
     */
    goToConfirm() {
        let { cart } = this.data
        let goods = [];
        cart.items.forEach(item => {
            if (item.check) {
                goods.push({skuId: item.skuId, count: item.count});
            }
        });

        if (goods.length == 0) return wx.showToast({ title: '您还没有选择商品哦', icon: 'none' });

        let data = { goods }
        wx.navigateTo({ url: '/pages/order_confirm/order_confirm?data=' + JSON.stringify(data) });
    },

    /**
     * 修改购物车商品项是否选中
     * @param {*} skuId 
     * @param {*} check 
     */
    async changeCheckItem(skuId, checked) {
        let { userInfo } = this.data
        let res = await http.request('/api/mall/cart/checkItem', {memberId: userInfo.id, skuId, checked})
        if(res.status) {
            this.getCarts()
        }
    },

    /**
     * 购物车商品选中
     * @param {*} e 
     */
    onChangeChecked(e) {
        let { id } = e.currentTarget.dataset
        let checked = e.detail
        this.changeCheckItem(id, checked)
    },

    /**
     * 购物车商品全选
     * @param {*} e 
     */
    async onChangeAllChecked(e) {
        let { userInfo, allChecked } = this.data
        this.setData({ allChecked: e.detail})

        let res = await http.request('/api/mall/cart/checkItemAll', {memberId: userInfo.id, checked: allChecked})
        if(res.status) {
            this.getCarts()
        }
    },

    /**
     * 编辑商品数量
     * @param {*} e 
     */
    async onChangeNum(e) {
        let { userInfo } = this.data
        let { skuId } = e.currentTarget.dataset
        let res = await http.request('/api/mall/cart/countItem', {memberId: userInfo.id, skuId, num: e.detail})
        if(res.status) {
            this.getCarts()
        }
    },

    /**
     * 删除购物车
     * @param {*} e 
     */
    async onDeleteCart(e) {
        let { confirm } = await showModel({content: '确定要删除？'})

        if(confirm){
            let { userInfo } = this.data
            let { skuId } = e.currentTarget.dataset
            let res = await http.request('/api/mall/cart/deleteItem', {memberId: userInfo.id, skuId})
            if(res.status) {
                this.getCarts()
            }
        }
       
    }

	
})